package cn.iluwen.airline.Mapper;

import cn.iluwen.airline.Entities.OrderDetail;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderDetailMapper {

    /**
     * 查询所有订单详情
     * @return 订单详情列表
     */
    @Select("SELECT * FROM order_details")
    List<OrderDetail> findAllOrderDetails();
    
    /**
     * 根据订单ID查询订单详情
     * @param orderId 订单ID
     * @return 订单详情列表
     */
    @Select("SELECT * FROM order_details WHERE order_id = #{orderId}")
    List<OrderDetail> findByOrderId(String orderId);
    
    /**
     * 根据ID查询订单详情
     * @param detailId 详情ID
     * @return 订单详情对象
     */
    @Select("SELECT * FROM order_details WHERE detail_id = #{detailId}")
    OrderDetail findById(String detailId);
    
    /**
     * 根据座位ID查询订单详情
     * @param seatId 座位ID
     * @return 订单详情对象
     */
    @Select("SELECT * FROM order_details WHERE seat_id = #{seatId}")
    OrderDetail findBySeatId(String seatId);
    
    /**
     * 添加订单详情
     * @param orderDetail 订单详情对象
     * @return 影响的行数
     */
    @Insert("INSERT INTO order_details (detail_id, order_id, flight_id, passenger_name, " +
            "passenger_id_card, seat_id, ticket_price, ticket_class, check_in_status) " +
            "VALUES (#{detailId}, #{orderId}, #{flightId}, #{passengerName}, " +
            "#{passengerIdCard}, #{seatId}, #{ticketPrice}, #{ticketClass}, #{checkInStatus})")
    int insert(OrderDetail orderDetail);
    
    /**
     * 批量添加订单详情
     * @param orderDetails 订单详情列表
     * @return 影响的行数
     */
    @Insert("<script>" +
            "INSERT INTO order_details (detail_id, order_id, flight_id, passenger_name, " +
            "passenger_id_card, seat_id, ticket_price, ticket_class, check_in_status) VALUES " +
            "<foreach collection='list' item='detail' separator=','>" +
            "(#{detail.detailId}, #{detail.orderId}, #{detail.flightId}, #{detail.passengerName}, " +
            "#{detail.passengerIdCard}, #{detail.seatId}, #{detail.ticketPrice}, #{detail.ticketClass}, #{detail.checkInStatus})" +
            "</foreach>" +
            "</script>")
    int batchInsert(@Param("list") List<OrderDetail> orderDetails);
    
    /**
     * 更新订单详情
     * @param orderDetail 订单详情对象
     * @return 影响的行数
     */
    @Update("UPDATE order_details SET order_id = #{orderId}, flight_id = #{flightId}, " +
            "passenger_name = #{passengerName}, passenger_id_card = #{passengerIdCard}, " +
            "seat_id = #{seatId}, ticket_price = #{ticketPrice}, ticket_class = #{ticketClass}, " +
            "check_in_status = #{checkInStatus} WHERE detail_id = #{detailId}")
    int update(OrderDetail orderDetail);
    
    /**
     * 更新值机状态
     * @param detailId 详情ID
     * @param seatId 座位ID
     * @param checkInStatus 值机状态
     * @return 影响的行数
     */
    @Update("UPDATE order_details SET seat_id = #{seatId}, check_in_status = #{checkInStatus} " +
            "WHERE detail_id = #{detailId}")
    int updateCheckIn(@Param("detailId") String detailId, @Param("seatId") String seatId, 
                     @Param("checkInStatus") Boolean checkInStatus);
    
    /**
     * 删除订单详情
     * @param detailId 详情ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM order_details WHERE detail_id = #{detailId}")
    int delete(String detailId);
    
    /**
     * 根据订单ID删除所有订单详情
     * @param orderId 订单ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM order_details WHERE order_id = #{orderId}")
    int deleteByOrderId(String orderId);
} 